package com.mxpio.erp.oa.controller;


import com.mxpio.erp.common.enums.BizExceptionEnums;
import com.mxpio.erp.common.exception.BizException;
import com.mxpio.erp.common.oa.entity.DocManager;
import com.mxpio.erp.common.oa.service.DocManagerService;
import com.mxpio.erp.common.util.AssertUtil;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "DocManagerController", description = "公文管理")
@RestController
@RequestMapping("/erp/oa/doc/")
public class DocManagerController {

    @Resource
    private DocManagerService docManagerService;

    @GetMapping("list")
    @Operation(summary = "公文管理列表", description = "公文管理列表（无分页）", method = "GET")
    public Result<List<DocManager>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        try {
            List<DocManager> list = docManagerService.list(DocManager.class, criteria);
            return Result.OK(list);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("page")
    @Operation(summary = "公文管理列表", description = "公文管理列表（分页）", method = "GET")
    public Result<Page<DocManager>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        try {
            Pageable page = PageRequest.of(pageNo - 1, pageSize);
            Page<DocManager> pageResult = docManagerService.listPage(DocManager.class, page, criteria);
            return Result.OK(pageResult);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取公文管理", description = "根据ID获取公文管理", method = "GET")
    public Result<DocManager> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        try {
            DocManager docManager = docManagerService.getById(DocManager.class, bizNo);
            return Result.OK(docManager);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @RequestMapping("add")
    @Operation(summary = "保存公文管理", description = "保存公文管理")
    public Result<DocManager> add(@RequestBody DocManager docManager) {
        try {
            return Result.OK(docManagerService.doSave(docManager));
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<DocManager> edit(@RequestBody DocManager docManager) {
        try {
            AssertUtil.notNull(docManager, BizExceptionEnums.PARAM_NOT_NULL);
            AssertUtil.notEmpty(docManager.getId(), BizExceptionEnums.PARAM_NOT_EMPTY, "id");
            DocManager dbData = docManagerService.getById(DocManager.class, docManager.getId());
            AssertUtil.notNull(dbData, BizExceptionEnums.ORDER_NOT_FOUND, docManager.getId());
            docManagerService.update(docManager);
            return Result.OK(docManager);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<DocManager> remove(@PathVariable(name = "ids", required = true) String ids) {
        try {
            String[] id = ids.split(",");
            for (String key : id) {
                docManagerService.delete(DocManager.class, key);
            }
            return Result.OK();
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

}